package org.springblade.modules.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 用户表 控制器
 *
 * @author BladeX
 * @since 2020-10-11
 */
@RestController
@AllArgsConstructor
@RequestMapping("blade-app/useraudit")
public class UserAuditController extends BladeController {

    private final IUserService userService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<User> detail(User params) {
        User user = userService.getById(params.getId());
        return R.data(user);
    }

    /**
     * 分页 用户表
     */
    @GetMapping("/list")
    public R<IPage<User>> list(Page page, User params) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        if (!StringUtils.isEmpty(params.getName())) {
            queryWrapper.and(wrapper -> wrapper.eq(User::getName, params.getName()));
        }
        queryWrapper.and(wrapper -> wrapper.eq(User::getShstatus, "1"));
        queryWrapper.orderByDesc(User::getCreateTime);
        IPage<User> pages = userService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 自定义分页 用户表
     */
    @GetMapping("/page")
    public R<IPage<User>> page(Page page, User params) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<User>();
        if (!StringUtils.isEmpty(params.getName())) {
            queryWrapper.and(wrapper -> wrapper.eq(User::getName, params.getName()));
        }
        queryWrapper.and(wrapper -> wrapper.eq(User::getShstatus, "1"));
        queryWrapper.orderByDesc(User::getCreateTime);
        IPage<User> pages = userService.page(page, queryWrapper);
        return R.data(pages);
    }

    /**
     * 新增 用户表
     */
    @PostMapping("/save")
    public R save(@RequestBody User params) {
        return R.status(userService.save(params));
    }

    /**
     * 修改 用户表
     */
    @PostMapping("/update")
    public R update(@RequestBody User params) {
        return R.status(userService.updateById(params));
    }

    /**
     * 新增或修改 用户表
     */
    @PostMapping("/submit")
    public R submit(@RequestBody User params) {
        return R.status(userService.saveOrUpdate(params));
    }


    /**
     * 删除 用户表
     */
    @PostMapping("/remove")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        User user = new User();
        user.setId(Long.valueOf(ids));
        user.setShstatus(2);
        return R.status(userService.updateById(user));
    }


}
